<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="工程师" prop="entName">
        <el-input
          v-model="queryParams.entName"
          placeholder="请输入工程师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="wbEntAuthList">
      <el-table-column label="工程师" align="center" prop="name" />
      <el-table-column label="电话号" align="center" prop="phone" />
      <el-table-column label="申请职位" align="center" prop="positionType">
        <template slot-scope="{ row }">
          <!-- {{ forrmatterEnmu(positionList, row.positionType + "") }} -->
          <div v-if="statusList1[row.applicantType]">
            {{ statusList1[row.applicantType].name }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="技能证书" align="center">
        <template slot-scope="{ row }">
          <image-preview
            :src="baseURL + row.skillCertificate"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>

      <el-table-column label="城市" align="center" prop="provinceString">
        <template slot-scope="{ row }">
          {{ fomatterAdressName(row.province)
          }}<span v-if="row.city">/{{ fomatterAdressName(row.city) }}</span
          ><span v-if="row.county">/{{ fomatterAdressName(row.county) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="auditType">
        <template slot-scope="{ row }">
          <div v-if="row.auditType == 1" style="color: #e6a23c">
            {{ forrmatterEnmu(statusList, row.auditType) }}
          </div>
          <div v-if="row.auditType == 2" style="color: #67c23a">
            {{ forrmatterEnmu(statusList, row.auditType) }}
          </div>
          <div v-if="row.auditType == 3" style="color: #f56c6c">
            {{ forrmatterEnmu(statusList, row.auditType) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" />

      <el-table-column
        label="审核时间"
        align="center"
        prop="updateBy"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.updateBy }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="审核人"
        align="center"
        prop="updateTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="detailHandler(scope.row)"
            >查看</el-button
          >

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.auditType == 1"
            @click="handleAudit(scope.row)"
            v-hasPermi="['wb:wbEngineer:pass']"
            >通过</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.auditType == 1"
            @click="handleAudit1(scope.row, 1)"
            v-hasPermi="['wb:wbEngineer:reject']"
            >驳回</el-button
          >
          <!-- <el-button size="mini" type="text" @click="delHandler(scope.row)"
            v-hasPermi="['wb:wbEngineer:delete']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业认证对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      v-if="false"
    >
      <el-form v-if="optionType == 1">
        <el-form-item label="驳回原因" prop="entName" class="form-item-row">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :inline="true"
        class="dialog-form"
        v-else
      >
        <el-form-item label="企业名" prop="entName" class="form-item-row">
          <el-input
            v-model="form.entName"
            placeholder="请输入企业名"
            style="width: 400px"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人/法人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="所在地址:">
          <provice-select
            :isShowDistrict="true"
            :defaultCity="defaultCity"
            @changeValue="changeValue"
          ></provice-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="entAddress" class="form-item-row">
          <el-input
            v-model="form.entAddress"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="描述" prop="remark" class="form-item-row">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          label="营业执照"
          prop="businessLicense"
          class="form-item-row"
        >
          <image-upload v-model="form.businessLicense" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button v-if="optionType == 1" type="primary" @click="submitForm"
          >驳回</el-button
        >
        <el-button v-else type="primary" @click="submitForm"
          >审核通过</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="提示" :visible.sync="open2" width="400px" append-to-body>
      <el-form>
        <el-form-item
          label="驳回原因"
          prop="causeOfRejection"
          class="form-item-row"
        >
          <el-input
            v-model="rejectedObj.causeOfRejection"
            type="textarea"
            placeholder="请输入驳回原因"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rejected">确定</el-button>
        <el-button @click="open2 = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 维修师傅详情 -->
    <el-dialog title="详情" :visible.sync="isShow" width="800px" append-to-body>
      <el-descriptions :column="2">
        <el-descriptions-item label="工程师">{{
          detailForm.name
        }}</el-descriptions-item>
        <el-descriptions-item label="电话号">{{
          detailForm.phone
        }}</el-descriptions-item>
        <el-descriptions-item label="申请职位">
          <!-- {{
            forrmatterEnmu(positionList, detailForm.positionType + "")
          }} -->
          <div v-if="statusList1[detailForm.applicantType]">
            {{ statusList1[detailForm.applicantType].name }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="审核状态">
          {{ forrmatterEnmu(statusList, detailForm.auditType) }}
        </el-descriptions-item>
        <el-descriptions-item label="申请时间">{{
          detailForm.createTime
        }}</el-descriptions-item>

        <el-descriptions-item label="城市">
          {{ fomatterAdressName(detailForm.province)
          }}<span v-if="detailForm.city"
            >/{{ fomatterAdressName(detailForm.city) }}</span
          ><span v-if="detailForm.county"
            >/{{ fomatterAdressName(detailForm.county) }}</span
          >
          <span>{{ detailForm.addr }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="技能照片" span="2">
          <el-upload
            :action="baseUrl"
            list-type="picture-card"
            class="enter-upload"
            :on-preview="handlePictureCardPreview"
            :disabled="true"
            :limit="1"
            :file-list="skillCertificate"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-descriptions-item>
      </el-descriptions>

      <!-- 照片 -->
      <el-dialog :visible.sync="dialogVisible" append-to-body>
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import Engineer from "@/api/wb/wbEngineer";
import ProviceSelect from "@/components/ProviceSelect/index.vue";
import { fomatterAdressName, forrmatterEnmu } from "@/utils/index.js";

export default {
  name: "WbEntAuth",
  components: { ProviceSelect },
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业认证表格数据
      wbEntAuthList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      rejectedObj: {
        id: "",
        causeOfRejection: "",
        auditType: 3,
        ifRead: 1,
      },
      baseUrl: process.env.VUE_APP_BASE_API,
      skillCertificate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        entName: null,
        entType: null,
        contact: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      detailForm: {},
      isShow: false,

      defaultCity: {},
      optionType: 0,
      statusList: [
        {
          value: 1,
          name: "待审核",
        },
        {
          value: 2,
          name: "已通过",
        },
        {
          value: 3,
          name: "已驳回",
        },
      ],
      statusList1: [
        {
          value: 0,
          name: "",
        },
        {
          value: 1,
          name: "消防设施操作员（监控方向）",
        },
        {
          value: 2,
          name: "消防设施操作员（维保方向）",
        },
        {
          value: 3,
          name: "一级注册消防工程师",
        },
      ],

      positionList: [],
      baseURL: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    this.getList();
    this.getPositionList();
  },
  methods: {
    fomatterAdressName,
    forrmatterEnmu,
    /** 查询企业认证列表 */
    getList() {
      this.loading = true;
      Engineer.engineerList(this.queryParams).then((response) => {
        this.wbEntAuthList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取工程师职位
    getPositionList() {
      this.positionList = [];
      Engineer.positionList().then((res) => {
        if (res.code == 200) {
          this.positionList = res.data.map((item) => {
            return {
              value: item.dictValue,
              name: item.dictLabel,
            };
          });
        }
      });
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    /**查看详情 */
    detailHandler(row) {
      this.isShow = !this.isShow;
      this.detailForm = row;
      if (row.skillCertificate) {
        let skillCertificate1 = row.skillCertificate.split(",");
        this.skillCertificate = skillCertificate1.map((item, index) => {
          return {
            name: index,
            url: this.baseUrl + item,
          };
        });
      } else {
        this.skillCertificate = [];
      }
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        entName: null,
        entAddress: null,
        entType: null,
        businessLicense: null,
        contact: null,
        phoneNumber: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wb/wbEntAuth/export",
        {
          ...this.queryParams,
        },
        `wbEntAuth_${new Date().getTime()}.xlsx`
      );
    },
    handleAudit(row, type) {
      let _this = this;
      this.$modal
        .confirm('是否确认审核工程师为"' + row.name + '"的数据项？')
        .then(function () {
          let auditType;
          if (type) {
            auditType = 3;
          } else {
            auditType = 2;
          }
          Engineer.engineerCheck({ id: row.id, auditType }).then((res) => {
            if (res.code == 200) {
              _this.getList();
              _this.$modal.msgSuccess("审核成功");
            }
          });
        })
        .then(() => {})
        .catch(() => {});
    },

    rejected() {
      Engineer.engineerCheck(this.rejectedObj).then((res) => {
        if (res.code == 200) {
          this.open2 = false;

          this.$message.success("审核成功");
          this.getList();
        }
      });
    },

    handleAudit1(row, type) {
      this.open2 = true;
      this.rejectedObj.id = row.id;
      this.rejectedObj.causeOfRejection = "";
    },

    delHandler(row) {
      Engineer.delEngineer(row.id).then((res) => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.getList();
        }
      });
    },
  },
};
</script>
<style lang="css" scoped>
::v-deep .enter-upload .el-upload--picture-card {
  display: none;
}
</style>
